package com.ostrich.lib_common.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/6 08:07
 * @Description : RetrofitManager
 */
public class RetrofitManager {
    private RetrofitManager(){}
    private static RetrofitManager retrofitManager;
    public static RetrofitManager getInstance(){
        if(retrofitManager == null){
            synchronized (RetrofitManager.class){
                if(retrofitManager == null){
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }
    private Retrofit mRetrofit;


    public Retrofit getRetrofit() {
        if(mRetrofit == null){
            createRetrofit();
        }


        return mRetrofit;
    }

    private void createRetrofit() {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new TokenInterceptor())
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))

                .build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl("http://10.161.9.80:8787")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(LiveDataCallAdapterFactory.create()) //livedata适配器
                .build();
    }


}
